﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Media;
using MyFloridaHouse.libSvOpenDistricts.MapShapes;
using MyFloridaHouse.ServiceConn.DistrBldrSvcRef;

namespace MyFloridaHouse.libSvOpenDistricts.ViewModel
{
    
    public partial class ViewModelBase
    {

        private ObservableCollection<MapShapeLayer> _mapLayerList = new ObservableCollection<MapShapeLayer>();
        public ObservableCollection<MapShapeLayer> mapLayerList
        {
            get { return _mapLayerList; }
            set { if (_mapLayerList != value) { _mapLayerList = value; NotifyPropertyChanged("mapLayerList"); } }
        }

        public bool isSelectedBuildLayerHidden
        {
            // depends on buildLayerListSelectedIndex 
            get 
            {
                if (buildLayerList == null)
                    return true;
                if (buildLayerListSelectedIndex < 0)
                    return true;
                if (buildLayerListSelectedIndex > buildLayerList.Count - 1)
                    return true;
                return !(buildLayerList[buildLayerListSelectedIndex].inZoomRange & buildLayerList[buildLayerListSelectedIndex].showLayer);
            }
            set { }
        }


        private int _buildLayerListSelectedIndex = -1;
        public int buildLayerListSelectedIndex
        {
            get { return _buildLayerListSelectedIndex; }
            set
            {
                if (_buildLayerListSelectedIndex != value)
                {
                    _buildLayerListSelectedIndex = value;

                    // when we change the build layer, we need to make sure the layer is being shown
                    NotifyPropertyChanged("buildLayerListSelectedIndex");
                    NotifyPropertyChanged("isSelectedBuildLayerHidden");

                }
            }
        }

        private ObservableCollection<MapShapeLayer> _buildLayerList = new ObservableCollection<MapShapeLayer>();
        public ObservableCollection<MapShapeLayer> buildLayerList
        {
            get { return _buildLayerList; }
            set { if (_buildLayerList != value) { _buildLayerList = value; NotifyPropertyChanged("buildLayerList"); } }
        }



        public void InitializeLayers(ObservableCollection<LayerDTO> layerList)
        {

            List<MapShapeLayer> tempMapLayerList = new List<MapShapeLayer>();

            var tempMapLayerQuery = (from lr in layerList
                                     where lr.isDisplayLayer == true
                                    orderby lr.displayOrder descending
                                    select lr);

            foreach (var layer in tempMapLayerQuery )
            {

                // add this layer to the map
                MapShapeLayer newLayer = new MapShapeLayer(serviceDAL, myXamlMainMap, layer, MapShapeLayer.enuMLayerAddLocation.top, false, regionLabel, regionFill);
                newLayer.SetRegionDetails(1.0, newLayer.lineStroke, 3.0, new SolidColorBrush(Colors.Magenta));

                // all layers coming from the layerlist at startup are predefined regions
                // regions that are built using the interface are not predefined (i.e. districts)
                newLayer.predefinedRegions = true;

                newLayer.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(layerPropertyChangedEventHandler);
                tempMapLayerList.Add(newLayer);
            }

            // now reverse the order for the layer control display
            var tmpLayerQuery = (from lr in tempMapLayerList
                                 where lr.isDisplayLayer == true
                                 orderby lr.displayOrder 
                                select lr);

            foreach (var layer in tmpLayerQuery)
            {
                mapLayerList.Add(layer);
            }


            //todo: Configure layer at startup from config settings or file
            // hard code starting with county layer turned on
            foreach (var lyr in mapLayerList)
            {
                if (lyr.layerAbbr == "CTY")
                    lyr.showLayer = true;
            }

            buildLayerList = new ObservableCollection<MapShapeLayer>();

            var tmpConfigLayerQuery = (from lr in mapLayerList
                            where lr.isConfigLayer
                            orderby lr.displayOrder
                            select lr);

            foreach (var lyr in tmpConfigLayerQuery)
            {
                buildLayerList.Add(lyr);
            }

            var tmpBuildLayerQuery = (from lr in mapLayerList
                            where lr.isBuildLayer & !lr.isConfigLayer
                            orderby lr.displayOrder descending
                            select lr);

            foreach (var lyr in tmpBuildLayerQuery)
            {
                buildLayerList.Add(lyr);
            }
            
            buildLayerListSelectedIndex = 0;

        }


        // link properties at this level that are dependent on layer properties
        void layerPropertyChangedEventHandler(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "inZoomRange") NotifyPropertyChanged("isSelectedBuildLayerHidden");
            if (e.PropertyName == "showLayer") NotifyPropertyChanged("isSelectedBuildLayerHidden");
        }


        public void ShowLayer(int layerIndex)
        {
            mapLayerList[layerIndex].showLayer = true;
        }

    }
}
